<?php

namespace App\Imports;


use App\Models\dispatch\MaterialModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;

class StockImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $this->result->error = [];


        foreach ($rows as $k => $row) {
            if($k < 1) continue;

            if($row[2] == ''){
                $this->result->error[] = '第'.$k.'行，物品名称不能为空';
                continue;
            }
            if(is_int($row[11])){
                $enter_date = $row[11] ? Date::excelToDateTimeObject($row[11])->format('Y-m-d') : NULL;
            }else{
                $enter_date = $row[11];
            }
            if(is_int($row[14])){
                $out_date = $row[14] ? Date::excelToDateTimeObject($row[14])->format('Y-m-d') : NULL;
            }else{
                $out_date = $row[14];
            }

            $lines[] = [
                'type' => $row[0],
                'code' => $row[1],
                'name' => $row[2],
                'spe' => $row[3],
                'model' => $row[4],
                'color' => $row[5],
                'unit' => $row[6],
                'number' => $row[7],
                'price' => $row[8],
                'last_month_number' => $row[9],
                'last_month_amount' => $row[10],
                'enter_date' => $enter_date,
                'enter_number' => $row[12],
                'enter_amount' => $row[13],
                'out_date' => $out_date,
                'out_number' => $row[15],
                'out_amount' => $row[16],
                'save_number' => $row[17],
                'save_amount' => $row[18]
            ];
        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
